define(
	[
		'require',
		'./totalranking_ctrl'
	],
	function(require){
		 function actionFormatter(value, row, index) {
	        return '<div class="" style="text-align:center;word-break:break-all;">'+APM.util.escape(value)+'</div>';
	    }
	    var numData = [];
		var Controller = require('./totalranking_ctrl').extend({
			formTitle: "异常交易排行",
			arrayData: Ember.A(['success_rate']),

			formTopArray: Ember.A([{"name":"top10","id":"10"},{"name":"top50","id":"50"},{"name":"top100","id":"100"}]),

			columns: [
			  [  {
			    "field": "responseCodeString",
			    formatter:actionFormatter,
			    "title": "返回码",
			    "colspan": 1,
			    "rowspan": 1
			    // "sortable": true
			  }, 
			  {
			  	 "field": "isError",
			    "title": "错误数",
			    "colspan": 1,
			    "rowspan": 1
			  }],
			],
			//根据数据来填充表格里的内容
			fillTableContent: function(node,index){
				$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
				var tableTr = $(".table-left-container .tbody .table-left-tr").eq(index);
				var num = ((node.response.length)*50);
				if(index == 0){
					num = num - 1;
				}
				$(".table-left-container .tbody .table-left-tr").eq(index).css({"height":num,"line-height":num+'px'});
				if(node.typeName){
					$("<div class='table-left-td'><div class='word-break'>"+node.typeName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td'>-</div>").appendTo(tableTr);
				}
				if(node.applicationName){
					$("<div class='table-left-td'><div class='word-break'>"+node.applicationName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td'>-</div>").appendTo(tableTr);
				}

				if(node.serviceName){
					$("<div class='table-left-td'><div class='word-break pull-left' >"+node.serviceName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td pull-left'>-</div>").appendTo(tableTr);
				}
				if(node.count){
					$("<div class='table-left-td'><div class='word-break pull-right'>"+node.count+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td pull-right'>-</div>").appendTo(tableTr);
				}
				if(node["success_rate"]){
					$("<div class='table-left-td'><div class='word-break pull-right'>"+node["success_rate"]+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td pull-right'>-</div>").appendTo(tableTr);
				}
				$(".table-left-container .tbody .table-left-td").css("width","20%");
				$(".table-left-container .tbody .table-left-tr").eq(index).find(".word-break").css("marginTop",(num-$(".word-break").eq(index).height())/2);
			},
			//根据数据插入表格头部
			fillTableHeader: function(){
				$(".table-left-container .thead").find("div").remove();
				$(".table-left-container .thead").append("<div class='clearfix table-left-tr'></div>");
				var tableTr = $(".table-left-container .thead .table-left-tr");

				$("<div class='table-left-td pull-left'>交易名称</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>业务系统</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>服务节点</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>总交易量</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>成功率%</div>").appendTo(tableTr);
				$(".table-left-container .thead .table-left-td").css("width","20%");

				// $(".table-left-container .thead .table-left-tr").css({"margin-left":"1px"});
				// setTimeout(function(){
				// 	$(".fixed-table-container thead th .th-inner").addClass("active");
				// },0)
				$(".table-left-container .thead").css({"height":"40px","line-height":"40px"});
			},
			formTopValueChange: Ember.observer("formTopValue",function(){
				this.reportformData();
			}),
			reportformData: function(){
				numData = [];
				var ctrl = this;
				var params = {
					"startDate": +this.get("startDate"),
					"endDate": +this.get("endDate"),
					"top": ctrl.get("formTopValue"),
				}
				var newArray = [],
					newObject;

				ctrl.set("tipTitle","");

				return this.store.ajax("GET", "report/trade/fail",params)
				.then(function(response){
					var performances = response.performances;

					//图表数据数据
					var chartsData = [];
					
					if(performances[0]){
						ctrl.fillTableHeader();
						$(".table-left-container .tbody").find("div").remove();
						performances.forEach(function(node,index){
							
							ctrl.fillTableContent(node,index);
							numData.push(node["response"].length);
							node["response"].forEach(function(app){

								newArray.push(app);
							});
							node["typeName"] = node["typeName"] +"@"+ node["applicationName"] +"@"+ node["serviceName"];
						});
					}else{
						ctrl.set("tipTitle","这段时间内暂无数据");
						$(".table-left-container .tbody").find("div").remove();
						$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
						$(".table-left-container .tbody .table-left-tr").css("height","49px");
					
						ctrl.fillTableHeader();
						chartsData = [];
					}

					var j = 0;
					ctrl.set("chartsData",performances);
					ctrl.set("groupData",newArray);
					
					setTimeout(function(){
						numData.forEach(function(node,index){
							if(index%2 == 0){
								for(var i=0;i<node;i++){

									$(".fixed-table-container tbody tr").eq(j+i).find("td").css("background"," rgba(0,0,0,.25)");
									
								}
							
							}else{
								for(var i=0;i<node;i++){
									$(".fixed-table-container tbody tr").eq(j+i).find("td").css("background"," rgba(0,0,0,.12)");
								}
							}
							j = j+node;
						});
						var width = $(".table-wrapper").width();
						$(".fixed-table-container tbody .no-records-found td").css({"textAlign":"left"}).html('');
						$(".fixed-table-container tbody .no-records-found td").append($("<div style='position:absolute;left:"+(-((width/2-(width*0.3))+24))+"px;top:54px;'>没有数据</div>"))
					},0);
				});
			},
			formTopValue:10,
			url:"report/trade/fail/download",
			actions: {
			
				export: function(){
					var ctrl = this,
						appId = ctrl.get("applicationValue"),
						startDate = +ctrl.get("startDate"),
						endDate = +ctrl.get("endDate"),
						top = ctrl.get("formTopValue");
						
					confirmDialog("确定要导出报表吗？")
					.then(function(){
						var url="/front/rest/apm/"+ctrl.get("url")+"?top="+top+"&startDate="+startDate+"&endDate="+endDate+"&token="+encodeURIComponent($.cookie("token"));
						window.location.href =url;
					});
				}
			}
		});

		APM.FormAbnormalController = Controller;
		return Controller;
	}
);